require_relative 'sample_tlv'
require 'test/unit'
require 'lib/smppclient/pdu/tlv/tlv_factory'
require 'lib/smppclient/util/byte_buffer'

class TestTlvFactory < MiniTest::Unit::TestCase

  include SmppClient::Util
  include SmppClient::Pdu::Tlv

  def test_parse
    bb = ByteBuffer.new
    bb.append_short(0x0422)
    bb.append_short(0x0001)
    bb.append_byte(10)

    mas = TlvFactory.parse(bb)
    assert_equal(Tag::MS_AVAILABILITY_STATUS, mas.tag)
    assert_equal(1, mas.length)
    assert_equal(10, mas.value)
    assert_equal('042200010a', mas.buffer.to_s)
  end

  def test_parse_tag_not_found
    bb = ByteBuffer.new
    bb.append_short(0x3333)
    bb.append_short(0x0001)
    bb.append_byte(10)

    assert_raises(TlvError) { TlvFactory.parse(bb) }
  end

  def test_add_remove
    bb = ByteBuffer.new
    bb.append_short(SampleTlv::TAG)
    bb.append_short(0x0001)
    bb.append_byte(10)

    TlvFactory.add('SAMPLE_TLV', SampleTlv::TAG, SampleTlv)

    st = TlvFactory.parse(bb)
    assert_equal(SampleTlv::TAG, st.tag)
    assert_equal(1, st.length)
    assert_equal(10, st.value)
    assert_equal('555500010a', st.buffer.to_s)

    TlvFactory.remove(SampleTlv::TAG)
  end

  def test_add_same_tag_twice
    assert_raises(TlvError) {
      TlvFactory.add('SAMPLE_TLV', Tag::MS_AVAILABILITY_STATUS, SampleTlv)
    }
  end

  def test_remove_not_existing_tag
    TlvFactory.remove(0x9876)
  end

end
